package com.edmendst.xmall.controller;

import com.edmendst.xmall.dto.AdminRegisterAndLogin;
import com.edmendst.xmall.service.AdminService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mjz
 */
@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 管理员注册
    @PostMapping
    public String register(@RequestBody AdminRegisterAndLogin adminRegisterAndLogin) {
        return adminService.register(adminRegisterAndLogin);
    }

    @PostMapping("/session")
    // 管理员登录
    public String login(@RequestBody AdminRegisterAndLogin adminRegisterAndLogin, HttpServletResponse response) {
        String token = adminService.login(adminRegisterAndLogin);
        if (token != null) {
            response.setHeader("Authorization", token);
        } else {
            return "登录失败";
        }
        return token;
    }



}
